package com.xzy.urp.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzy.urp.common.annotations.EnableLoginHandle;
import com.xzy.urp.common.exception.BusinessException;
import com.xzy.urp.common.result.R;
import com.xzy.urp.common.result.ResponseEnum;
import com.xzy.urp.pojo.vo.UserInfoVO;
import com.xzy.urp.pojo.vo.UserVO;
import com.xzy.urp.service.UserInfoService;
import com.xzy.urp.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/admin/userInfo")
@Api(tags = "用户登录信息管理接口")
public class UserInfoController {


    private final UserInfoService userInfoService;

    @Autowired
    public UserInfoController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    //   @EnableLoginHandle(identity = {3})
    @ApiOperation("用户注册(管理员权限)")
    @PostMapping("/register")
    public R register(@ApiParam("注册对象")
                      @RequestBody UserVO registerVO) {



        //这里仅注册用户信息
        //学籍注册，教师信息注册需要重新请求
        userInfoService.register(registerVO);


        return R.ok().message("注册成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@ApiParam
                   @RequestBody UserVO loginVO) {

        UserInfoVO login = userInfoService.login(loginVO);

        return R.ok().message("登录成功").data("loginVO",login);


    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {

        String token = request.getHeader("token");  // 获取令牌(前端将token放入请求头中)


        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }


  //  @EnableLoginHandle(identity = {0,1,2,3})
    @ApiOperation("修改密码")
    @PostMapping("/UpdatePwd")
    public R updatePwd(HttpServletRequest request,
                       @ApiParam(value = "旧密码", required = true)
                       @RequestParam("oldPwd")
                               String oldpwd,
                       @ApiParam(value = "新密码", required = true)
                       @RequestParam("newPwd")
                               String newpwd
    ){
        //JWT获取id
        Long userId = JwtUtils.getUserId(request.getHeader("token"));


        userInfoService.updatePwd(userId,oldpwd,newpwd);




        return R.ok().message("修改成功");
    }


   // @EnableLoginHandle(identity = 3)
    @ApiOperation(value = "excel表批量导入新用户（管理员权限）")
    @PostMapping("/Excel/import")
    public R userExcel(@ApiParam(value = "Excel数据字典文件",required = true)
                      @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            userInfoService.importData(inputStream);

            return R.ok().message("数据导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException((ResponseEnum.UPLOAD_ERROR),e);
        }
    }
    @ApiOperation("个人信息维护")
    public R updateInfo(@ApiParam
                            @RequestBody UserInfoVO UserInfoVO) {

        userInfoService.updateInfo(UserInfoVO);


        return R.ok().message("修改成功");
    }
}

